﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp10
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        public Form3(int id,string title , string author)
        {
            InitializeComponent();
            this.id = id;
            textBox1.Text = title;
            textBox2.Text = author;
       
            
        }
        private int id;
        //确定
        private void button1_Click(object sender, EventArgs e)
        {
            var title = textBox1.Text;

            var author = textBox2.Text;
            var createtime = textBox3.Text.ToString();

         
            //修改
            if (id > 0)
            {
                if (!string.IsNullOrEmpty(title))
                {
                    var sql = string.Format("update Articles set Title='{0}',Author='{1}',CreatedTime='{2}' where Id={3}", title, author, createtime, this.id); ;
                    var res = DbHelper.AddOrUpdateOrDelete(sql);

                    if (res == 1)
                    {
                        MessageBox.Show("修改成功", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                }
            }
            //新增
            else
            {
                if (!string.IsNullOrEmpty(title))
                {
                    var sql = string.Format("insert into Articles(Title, Author, CreatedTime) values('{0}','{1}','{2}')", title, author, createtime);
                    var res = DbHelper.AddOrUpdateOrDelete(sql);

                    if (res == 1)
                    {
                        MessageBox.Show("添加成功", "提示");
                        this.Close();

                    }
                    else
                    {
                        MessageBox.Show("添加失败", "提示");
                        this.Close();

                    }
                }
            }

        }
    }
}
